//## 공정  정보 저장 ####################
function fnSave()
{
	if (!$("#rank").val())
	{
		alert("작업을 선택해 주세요!");
		return;		
	}
	if (!$("#lotNo").val() || !$("#lotSubNo").val())
	{
		alert("작업을 선택해 주세요!");
		return;
	}
	var valUrl  = '/action/pop_jobOrder/save.asp';
		var param = "lotNo="+$("#lotNo").val();
		param += "&lotSubNo="+$("#lotSubNo").val();
		param += "&rank="+$("#rank").val();
		param += "&processCd="+$("#processCd").val();
		param += "&userId="+$("#userId").val();
		param += "&workDt="+$("#workDt").val();
		param += "&startDt="+$("#startDt").val();
		param += "&endDt="+$("#endDt").val();
		param += "&etc="+encodeURIComponent($("#etc").val());
		param += "&execTime="+encodeURIComponent($("#execTime").val());
		param += "&working="+encodeURIComponent($("#working").val());
		param += "&type="+$("#type").val();
		param += "&goodQty="+$("#goodQty").val();
		param += "&badQty="+$("#badQty").val();
		param += "&orderMemo="+encodeURIComponent($("#orderMemo").val());
		param += "&flag="+$("#flag").val();
		//alert(param);
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 공정 정보 그리드 표시 ####################
var G_curPage01 = 1;

function grid01List(page)
{
	
	var gridNo = 1;
	var xmlNode = "LotDetails";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage01;
	if (page == undefined) page = 1;
	else G_curPage01 = page;
	var limit = 50;

	var valUrl = "/action/pop_jobOrder/list.asp";
	var Parm = "findLotNo="+$("#findLotNo").val();
		Parm += "&findProcessCd="+$("#findProcessCd").val();
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",,,,,,,,,left,,".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,,,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var ttlGood = 0;
			var ttlBad = 0;
			var ttlQty = 0;
			$(xml).find(xmlNode).each(function(idx){
				
				if (k>startNo && k<=endNo)
				{
					var lotYear			= $(this).find('LotYear').text();
					var lotNo				= $(this).find('LotNo').text();
					var lotSubNo		= $(this).find('LotSubNo').text();
					var processCd		= $(this).find('ProcessCd').text();
					var rank				= $(this).find('Rank').text();
					var userNm			= $(this).find('UserNm').text();
					var workDt			= $(this).find('WorkDt').text();
					var working			= $(this).find('Working').text();
					var execTime		= $(this).find('ExecTime').text();
					var goodQty		= $(this).find('GoodQty').text();
					var badQty			= $(this).find('BadQty').text();
					var totalQty			= $(this).find('TotalQty').text();
					var etc					= $(this).find('Etc').text();
					var memo				= $(this).find('Memo').text();
					
					var arrVal = new Array();
					arrVal[0] = "<p>"+no+"</p>";
					arrVal[1] = "<p>"+userNm+"</p>";
					arrVal[2] = "<p>"+workDt+"</p>";
					arrVal[3] = "<p>"+execTime+"</p>";
					arrVal[4] = "<p>"+working+"</p>";
					arrVal[5] = "<p>"+etc+"</p>";
					arrVal[6] = "<p>"+goodQty+"</p>";
					arrVal[7] = "<p>"+badQty+"</p>";
					arrVal[8] = "<p>"+totalQty+"</p>";
					arrVal[9] = "<p>"+memo+"</p>";
					ttlGood = parseInt(ttlGood,10)+parseInt(goodQty,10);
					ttlBad = parseInt(ttlBad,10)+parseInt(badQty,10);
					ttlQty = parseInt(ttlQty,10)+parseInt(totalQty,10);
					
					var curDay = fnGetToday();
					if (curDay == workDt) bgColor = "#f0faff";
					else  bgColor = "#fff";

					var link = "fnSetLotDetail('"+lotYear+"',  '"+lotNo+"',  '"+lotSubNo+"',  '"+rank+"')";

					//gridList = fnDrawCell(lotYear, lotNo, lotSubNo, processCd);
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				no = parseInt(endNo,10)-parseInt(k,10);
				k++;			
			});
			var bgColor = "#f0faff";
			arrVal = [ '','<p><b>TOTAL</b></p>','','','','',"<p><b>"+getNumWithComma(ttlGood)+"</b></p>","<p><b>"+getNumWithComma(ttlBad)+"</b></p>","<p><b>"+getNumWithComma(ttlQty)+"</b></p>",'' ];
			gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', '', cellLink);
			$("#"+"gridBody"+gridNo).prepend(gridList);
			$("#"+"gridBody"+gridNo).append(gridList);
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});

}


//## 공정 상세정보 표시 ####################
function fnSetLotDetail(lotYear, lotNo, lotSubNo, rank)
{
	var valUrl = "/action/pop_jobOrder/view.asp";
	var param = "lotYear="+lotYear;
		param += "&lotNo="+lotNo;
		param += "&lotSubNo="+lotSubNo;
		param += "&rank="+rank;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();			
			$("#rank").val($(xml).find("rank").text());
			$("#lotNo").val($(xml).find("lotNo").text());
			$("#lotSubNo").val($(xml).find("lotSubNo").text());
			$("#prdNm").val($(xml).find("prdNm").text());
			$("#workDt").val($(xml).find("workDt").text());
			$("#userId").val($(xml).find("userId").text());
			$("#working").val($(xml).find("working").text());
			$("#etc").val($(xml).find("etc").text());
			$("#execTime").val($(xml).find("execTime").text());			
			$("#goodQty").val($(xml).find("goodQty").text());
			$("#badQty").val($(xml).find("badQty").text());
			$("#totalQty").val($(xml).find("totalQty").text());			
			$("#orderMemo").val($(xml).find("orderMemo").text());
			$("#processCd").val($(xml).find("processCd").text());
			$("#flag").val($(xml).find("flag").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 공정 수량 자동 계산
function fnAutoCount()
{
	var good = $("#goodQty").val();
	var bad = $("#badQty").val();
	if (good =="") good = 0;
	if (bad == "") bad = 0;
	var total = parseInt(good,10) + parseInt(bad,10);
	$("#totalQty").val(total);	
}

//##  작업 기간 설정
function fnWorkDt(flag)
{
	if (flag == "dt")
	{
		$("#workDt").css("background-color","#FFF");
		$("#startDt").css("background-color","#f0faff");
		$("#endDt").css("background-color","#f0faff");
	}
	else
	{
		$("#workDt").css("background-color","#f0faff");
		$("#startDt").css("background-color","#FFF");
		$("#endDt").css("background-color","#FFF");
	}
}
//## 공정  정보 Reset 기능
function fnReset()
{
	$("#rank").val("");
	$("#lotNo").val("");
	$("#lotSubNo").val("");
	$("#prdNm").val("");
	$("#workDt").val("");
	$("#userId").val("");
	$("#working").val("");
	$("#etc").val("");
	$("#execTime").val("");		
	$("#startDt").val("");		
	$("#endDt").val("");	
	$("#goodQty").val("");
	$("#badQty").val("");
	$("#totalQty").val("");			
	$("#orderMemo").val("");			
	$("#processCd").val("");			
	$("#flag").val("Y");
	$("#workDt").css("background-color","#FFF");
	$("#startDt").css("background-color","#f0faff");
	$("#endDt").css("background-color","#f0faff");
}


//## 검색 Reset 기능
function fnSearchReset()
{
	//$("#findLotNo").val("");
	$("#findProcessCd").val("");
}